package com.lujianfei.waterpower.ui.power.powerAdd;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.lujianfei.waterpower.R;
import com.lujianfei.waterpower.base.BaseActivity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class PowerAddActivity extends BaseActivity implements PowerAddContract.View{

    public final static String KEY_ID = "id";
    PowerAddContract.Presenter presenter;
    EditText edit_readout;
    EditText edit_unit_price;
    EditText edit_remark;
    Button btn_date;
    ImageView iv_image;
    ImageView iv_delete;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_power_add);
        long id = getId();
        setTitleMiddleText(-1 == id ? "电费添加" : "电费编辑");
        setTitleRightImage(R.drawable.ic_ok);
        showTitleRightImage();
        initView();
        presenter = new PowerAddPresenter(this);
        presenter.loadData();

        initEvent();
    }

    private void initEvent() {
        iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.removePhoto();
            }
        });
        btn_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.openDatePicker();
            }
        });
        iv_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.openImagePicker();
            }
        });
    }

    private void initView() {
        edit_readout = findViewById(R.id.edit_readout);
        edit_unit_price = findViewById(R.id.edit_unit_price);
        btn_date = findViewById(R.id.btn_date);
        iv_image = findViewById(R.id.iv_image);
        iv_delete = findViewById(R.id.iv_delete);
        edit_remark = findViewById(R.id.edit_remark);
    }

    @Override
    public void onTitleRightImageClick() {
        super.onTitleRightImageClick();
        boolean save = presenter.save();
        if (save) {
            finish();
        }
    }

    @Override
    protected int getTitleBarResId() {
        return R.id.custom_titlebar;
    }

    @Override
    public long getId() {
        return getIntent().getLongExtra(KEY_ID,-1);
    }

    @Override
    public void setReadOut(String readout) {
        edit_readout.setText(readout);
    }

    @Override
    public void setDate(String month) {
        btn_date.setText(month);
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public double getReadOut() {
        return Double.parseDouble(edit_readout.getText().toString());
    }

    @Override
    public double getUnitPrice() {
        return Double.parseDouble(edit_unit_price.getText().toString());
    }

    @Override
    public void setUnitPrice(String unitPrice) {
        edit_unit_price.setText(unitPrice);
    }

    @Override
    public void setBitmap(Bitmap bitmap) {
        iv_image.setImageBitmap(bitmap);
    }

    @Override
    public void showDelete() {
        iv_delete.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideDelete() {
        iv_delete.setVisibility(View.GONE);
    }

    @Override
    public void setImageDefault() {
        iv_image.setImageResource(R.drawable.ic_add_photo);
    }

    @Override
    public void setRemark(String s) {
        edit_remark.setText(s);
    }

    @Override
    public String getRemark() {
        return edit_remark.getText().toString();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        presenter.onActivityResult(requestCode,resultCode,data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        presenter.onRequestPermissionsResult(requestCode,permissions,grantResults);
    }
}
